package com.umbacos.udm.client.view;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.umbacos.udm.client.arch.ImageButton;
import com.umbacos.udm.client.presenter.TrackerPresenter;

public class TrackerView extends Grid{

	// left side with play/stop buttons
	private final HorizontalPanel hpCommand=new HorizontalPanel();
	// central area with two rows
	private final Grid grInfo=new Grid(2, 1);
	// right side with info and delete buttons
	private final HorizontalPanel hpAction=new HorizontalPanel();
	
	private final HorizontalPanel hpBar=new HorizontalPanel();
	public final HTML htName=new HTML("");
	public final HorizontalPanel hpInfo=new HorizontalPanel();

	public final ImageButton ibPlay=new ImageButton("images/play.png", 100, 24, 32);
	public final ImageButton ibStop=new ImageButton("images/stop.png", 100, 24, 32);
	public final ImageButton ibInfo=new ImageButton("images/info.png", 100, 16, 16);
	public final ImageButton ibAmul=new ImageButton("images/amu_small.png", 100, 24, 32);
	public final ImageButton ibMeta=new ImageButton("images/lens.png", 100, 24, 32);
	public final ImageButton ibDele=new ImageButton("images/delete.png", 100, 24, 32);

	public TrackerView(){
		resize(1,3);
		getCellFormatter().setWidth(0, 0, "32px");
		getCellFormatter().setWidth(0, 2, "64px");
		getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		getCellFormatter().setAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		setStyleName("tableborder");
		setWidget(0, 0, hpCommand);
		setWidget(0, 1, grInfo);
		setWidget(0, 2, hpAction);

		hpBar.add(hpInfo);
		hpCommand.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hpAction.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		grInfo.setWidget(0, 0, htName);
		grInfo.setWidget(1, 0, hpBar);

		ibPlay.setTitle("Restart the download");
		ibStop.setTitle("Pause the download");
		ibMeta.setTitle("Show the tracker's info");
		ibDele.setTitle("Delete the tracker");
		
		ibInfo.removeStyleName("pointer");
	}
	
	public void setIcons(TrackerPresenter.TrackerType type, boolean isRunning){
		
		if (hpAction.getWidgetCount()==0)
			hpAction.add(ibDele);
		
		switch (type) {
		case WGET:
		case CTOR:
			hpCommand.clear();
			if (isRunning)
				hpCommand.add(ibStop);
			else
				hpCommand.add(ibPlay);	
			if (hpAction.getWidgetCount()<2)
				hpAction.insert(ibMeta, 0);
			break;
		case INFO:
			if (hpCommand.getWidgetCount()==0)
				hpCommand.add(ibInfo);
			break;
		case AMULE:
			if (hpCommand.getWidgetCount()==0)
				hpCommand.add(ibAmul);
			break;
		}
	}
}